package com.jcsz.gulimall.order.listener;

import com.jcsz.common.to.mq.SeckillOrderTo;
import com.jcsz.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @className: OrderSeckillListener
 * @author: 一把Po吉他
 * @date: 2022/11/19 15:56
 * @version: 1.0
 **/
@RabbitListener(queues = "order.seckill.order.queue")
@Component
@Slf4j
public class OrderSeckillListener {

    @Autowired
    private OrderService orderService;
    @RabbitHandler
    public void test(Message message, SeckillOrderTo seckillOrderTo, Channel channel) throws IOException {
        log.info("准备创建秒杀单的详细信息...");

        try {
            orderService.createSeckillOrder(seckillOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }

}
